package com.devg.file.client.core;

import com.alibaba.fastjson.JSONObject;
import com.devg.file.client.core.entity.FileClientConfig;
import com.devg.file.client.core.entity.FileResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class WebServiceRequest<T> {

  public static FileClientConfig fileClientConfig = null;


  private static final Logger log = LoggerFactory.getLogger(WebServiceRequest.class);

  public abstract FileResponse invoke(T data, FileClientConfig fileClientConfig, String requestType);

  public abstract List<FileResponse> invoke(List<T> data, FileClientConfig fileClientConfig, String requestType);

  public abstract FileResponse invoke(T data, String requestType);

  public abstract List<FileResponse> invoke(List<T> data, String requestType);

  protected void fillMultipartEntityBuilder(MultipartEntityBuilder builder, FileClientConfig config) {
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    builder.addTextBody("bucketName", config.getBucketName());
    builder.setCharset(Charset.forName("UTF-8"));
  }

  protected FileResponse invokeFile(MultipartEntityBuilder builder, FileClientConfig config, String requestType) {
    CloseableHttpClient httpClient = null;
    HttpResponse httpResponse = null;
    FileResponse fileResponse = new FileResponse();
    try {
      httpClient = HttpClients.createDefault();
      HttpPost httpPost = WebHttpPost.getInstance().getHttpPost(config, requestType);
      HttpEntity entity = builder.build();
      httpPost.setEntity(entity);
      //执行提交
      httpResponse = httpClient.execute(httpPost);
      int returnCode = httpResponse.getStatusLine().getStatusCode();
      fileResponse.setStatusCode(returnCode);
      if (returnCode == HttpStatus.SC_OK) {
        HttpEntity httpEntity = httpResponse.getEntity();
        byte[] json = EntityUtils.toByteArray(httpEntity);
        String responseJson = new String(json, "UTF-8");
        JSONObject jsonObject = JSONObject.parseObject(responseJson);
        String fileName = jsonObject.getString("fileName");
        String fileType = jsonObject.getString("fileType");
        String filePath = jsonObject.getString("filePath");
        String netPath = jsonObject.getString("netPath");
        Long fileSize = jsonObject.getLong("fileSize");
        String realFileName = jsonObject.getString("realFileName");
        String code = jsonObject.getString("code");
        String message = jsonObject.getString("message");
        fileResponse.setFileName(fileName);
        fileResponse.setRealFileName(realFileName);
        fileResponse.setFilePath(filePath);
        fileResponse.setNetPath(netPath);
        fileResponse.setFileType(fileType);
        fileResponse.setCode(code);
        fileResponse.setMessage(message);
        fileResponse.setFileSize(fileSize);
        //关闭流
        EntityUtils.consume(httpEntity);
      }
    } catch (Exception ex) {
      fileResponse.setStatusCode(500);
      fileResponse.setCode("500");
      fileResponse.setMessage(ex.getMessage());
      log.error("upload file exception", ex);
    } finally {
      if (null != httpResponse) {
        try {
          ((CloseableHttpResponse) httpResponse).close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (null != httpClient) {
        try {
          httpClient.close();
        } catch (IOException ex) {
          ex.printStackTrace();
        }
      }
    }
    return fileResponse;
  }
}
